
package com.borqs.music.ui.adapter;

import java.util.List;

import android.content.Context;
import android.os.RemoteException;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.borqs.music.R;
import com.borqs.music.model.SongInfo;
import com.borqs.music.ui.online.PersonalMusicListActivity;
import com.borqs.music.util.MusicUtils;

public class PersonalTrackListAdapter extends ArrayAdapter<SongInfo> {

    private static final int layoutId = R.layout.track_list_item;
    private final String mUnknownArtist;
    private LayoutInflater inflater;
    private PersonalMusicListActivity mActivity;

    private static class ViewHolder {
        public TextView line1;
        public TextView line2;
        public ImageView favorite_indicator;
        public ImageView play_indicator;
        public ImageView locate_indicator;
        public ImageView pop_menu;
    }

    public PersonalTrackListAdapter(Context context, List<SongInfo> items) {
        super(context, 0, items);
        inflater = LayoutInflater.from(context);
        mUnknownArtist = context.getString(R.string.unknown_artist_name);
    }

    @Override
    public long getItemId(int position) {
        return getItem(position).id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View returnView = null;
        ViewHolder vh = null;
        if (null == convertView) {
            returnView = inflater.inflate(layoutId, parent, false);
            vh = new ViewHolder();
            vh.line1 = (TextView) returnView.findViewById(R.id.line1);
            vh.line2 = (TextView) returnView.findViewById(R.id.line2);
            vh.play_indicator = (ImageView) returnView.findViewById(R.id.play_indicator);
            vh.locate_indicator = (ImageView) returnView.findViewById(R.id.locate_indicator);
            vh.favorite_indicator = (ImageView) returnView.findViewById(R.id.btn_favorite);
            vh.favorite_indicator.setVisibility(View.VISIBLE);
            vh.favorite_indicator.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SongInfo song = (SongInfo) v.getTag();
                    mActivity.updateFavoriteStatus(song);
                }
            });
            vh.pop_menu = (ImageView) returnView.findViewById(R.id.pop_menu);
            vh.pop_menu.setVisibility(View.VISIBLE);
            vh.pop_menu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.showContextMenu();
                }
            });
            returnView.setTag(vh);
        } else {
            returnView = convertView;
            vh = (ViewHolder) returnView.getTag();
        }

        SongInfo song = getItem(position);
        if (song.fileType.equalsIgnoreCase("mp3"))
            vh.line1.setText(song.title);
        else if (!TextUtils.isEmpty(song.fileName))
            vh.line1.setText(song.fileName.substring(0, song.fileName.lastIndexOf(".")));

        if (TextUtils.isEmpty(song.artist) || song.artist.equals(MediaStore.UNKNOWN_STRING)) {
            vh.line2.setText(mUnknownArtist);
        }else{
            vh.line2.setText(song.artist);
        }

        vh.favorite_indicator.setTag(song);
        if (song.isFavorite) {
            vh.favorite_indicator.setImageResource(R.drawable.indicator_ic_mp_favorite_hl_list);
        } else {
            vh.favorite_indicator.setImageResource(R.drawable.indicator_ic_mp_favorite_list);
        }
        if (!song.isNotInDevice) {
            vh.locate_indicator.setImageResource(R.drawable.indicator_ic_mp_sdcard_list);
            vh.locate_indicator.setVisibility(View.VISIBLE);
        } else {
            vh.locate_indicator.setVisibility(View.GONE);
        }

        long id = -1;
        if (MusicUtils.sService != null) {
            try {
                id = MusicUtils.sService.getAudioId();
            } catch (RemoteException ex) {
            }
        }
        if (song.id == id) {
            vh.play_indicator.setImageResource(R.drawable.indicator_ic_mp_playing_list);
            vh.play_indicator.setVisibility(View.VISIBLE);
        } else {
            vh.play_indicator.setVisibility(View.GONE);
        }
        return returnView;
    }

    public void setActivity(PersonalMusicListActivity newactivity) {
        mActivity = newactivity;
    }
}
